<template>
  <el-dialog :title="title" :visible.sync="visible" width="600px" append-to-body v-if="visible">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="工作人员" prop="memberId">
        <user-member-select v-model="form.memberId" placeholder="请选择工作人员"/>
      </el-form-item>
      <el-form-item label="踏查日期" prop="tcTime">
        <el-date-picker clearable
                        v-model="form.tcTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="踏查结果" prop="tcResult">
        <el-select v-model="form.tcResult" placeholder="请选择踏查结果">
          <el-option v-for="dict in dict.type.tc_result" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="内部图片" prop="imgIn">
        <image-upload v-model="form.imgIn"/>
      </el-form-item>
      <el-form-item label="外部图片" prop="imgOut">
        <image-upload v-model="form.imgOut"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>

</template>

<script>

import {addTc} from "@/api/duty/tc";

export default {
  name: "addTc",
  dicts: ['tc_result'],
  data() {
    return {
      buttonLoading: false,
      title: '新增踏查',
      visible: false,
      form: {},
      // 表单校验
      rules: {
        placeId: [
          {required: true, message: "踏查场所不能为空", trigger: "blur"}
        ],
        memberId: [
          {required: true, message: "工作人员不能为空", trigger: "blur"}
        ],
        tcTime: [
          {required: true, message: "踏查日期不能为空", trigger: "blur"}
        ],
        tcResult: [
          {required: true, message: "踏查结果不能为空", trigger: "blur"}
        ],
      }
    }
  },
  methods: {
    open(placeId) {
      this.loading = true;
      this.reset();
      this.form.placeId = placeId
      this.visible = true
    },
    cancel() {
      this.visible = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        placeId: undefined,
        memberId: undefined,
        tcTime: undefined,
        tcResult: undefined,
        imgIn: undefined,
        imgOut: undefined,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          addTc(this.form).then(res => {
            this.$modal.msgSuccess("新增成功");
            this.visible = false;
            this.$emit('update');
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      });
    }
  }
}
</script>

<style scoped>

</style>
